package com.bjc.sdx.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bjc.sdx.R;
import com.bjc.sdx.adapter.CustomerAdapter;
import com.bjc.sdx.adapter.MyBaseAdapter;
import com.bjc.sdx.fragment.Fragment1;
import com.bjc.sdx.fragment.Fragment2;
import com.bjc.sdx.fragment.Fragment3;
import com.bjc.sdx.model.ChildObj;
import com.bjc.sdx.model.Fruit;
import com.bjc.sdx.model.ParentObj;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyFragmentActivity extends FragmentActivity implements View.OnClickListener {

    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;

    private RadioGroup rGroup;

    private Fragment f1,f2,f3;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private BaseAdapter baseAdapter;

    private static final String TAG = "MyFragmentActivity";
//    private ExpandableListView expandableListView;
//    private CustomerAdapter customerAdapter;

    private ListView listView;
    private ArrayList<ParentObj> listData = new ArrayList<ParentObj>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_layout);

        /**
         * 拿到事务管理器并开启事务
         */
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        /**
         * 初始化按钮
         */
        rb1 = (RadioButton) findViewById(R.id.rb_1);
        rb2 = (RadioButton) findViewById(R.id.rb_2);
        rb3 = (RadioButton) findViewById(R.id.rb_3);

        rGroup = (RadioGroup) findViewById(R.id.rg);

        /**
         * 为三个按钮添加监听
         */
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);

        /**
         * 启动默认选中第一个
         */
        rGroup.check(R.id.rb_1);
        f1 = new Fragment1();
        transaction.replace(R.id.fl_content, f1);
        transaction.commit();



        // 顶部导航侧滑菜单的实现
        /*--------------------侧边栏的数据及View--------------------------*/
//        createGroupData();
//
//        createChildData();
//
//        expandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view_id);
//        customerAdapter = new CustomerAdapter(listData, this);
//        expandableListView.setAdapter(customerAdapter);
//        expandableListView.setGroupIndicator(null);
//
//        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
//
//            @Override
//            public void onGroupExpand(int groupPosition) {
//                for (int i = 0; i < customerAdapter.getGroupCount(); i ++) {
//                    if (i != groupPosition) {
//                        expandableListView.collapseGroup(i);
//                    }
//                }
//            }
//        });
//
//        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
//
//            @Override
//            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//                Toast.makeText(MyFragmentActivity.this, listData.get(groupPosition).getChilds().get(childPosition).getcName(), Toast.LENGTH_LONG).show();
//                return true;
//            }
//        });


        // 侧滑菜单栏的数据
        listView = findViewById(R.id.expandable_list_view_id);;
//        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
        baseAdapter = new MyBaseAdapter(this,getData());
        listView.setAdapter(baseAdapter);


        final DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
        //给按钮添加一个监听器
        findViewById(R.id.top_view_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 这里设置的方向要与layout里面的保持一致android:layout_gravity="end"
                //打开侧滑菜单
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                Log.w(TAG, "onDrawerSlide 侧边栏开始滑动");
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                Log.w(TAG, "onDrawerOpened  打开侧边栏");
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                Log.w(TAG, "onDrawerClosed 关闭侧边栏");
            }


            @Override
            public void onDrawerStateChanged(int newState) {
                Log.w(TAG, "onDrawerStateChanged 侧边栏状态发生改变 newState=" + newState);
            }
        });

        /*----------------------------------------------*/
    }

    @Override
    public void onClick(View v) {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        switch (v.getId()) {
            case R.id.rb_1 :
                /**
                 * 为了防止重叠，需要点击之前先移除其他Fragment
                 */
                hideFragment(transaction);
                f1 = new Fragment1();
                transaction.replace(R.id.fl_content, f1);
                transaction.commit();

                break;
            case R.id.rb_2 :
                hideFragment(transaction);
                f2 = new Fragment2();
                transaction.replace(R.id.fl_content, f2);
                transaction.commit();
                break;
            case R.id.rb_3 :
                hideFragment(transaction);
                f3 = new Fragment3();
                transaction.replace(R.id.fl_content, f3);
                transaction.commit();
                break;

            default :
                break;
        }
    }

    /*
     * 去除（隐藏）所有的Fragment
     * */
    private void hideFragment(FragmentTransaction transaction) {
        if (f1 != null) {
            //transaction.hide(f1);隐藏方法也可以实现同样的效果，不过我一般使用去除
            transaction.remove(f1);
        }
        if (f2 != null) {
            //transaction.hide(f2);
            transaction.remove(f2);
        }
        if (f3 != null) {
            //transaction.hide(f3);
            transaction.remove(f3);
        }


    }


    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "测试1");
        map.put("info", "Toast");
        map.put("img", R.drawable.grape);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "测试2");
        map.put("info", "AlertDialog");
        map.put("img", R.drawable.apple);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "测试3");
        map.put("info", "PopupWindow");
        map.put("img", R.drawable.banana);
        list.add(map);
        return list;
    }

//    /*==============策划导航栏使用函数=============================*/
//
//    private void createGroupData() {
//        ParentObj p1 = new ParentObj();
//        p1.setpName("HP");
//        listData.add(p1);
//
//        ParentObj p2 = new ParentObj();
//        p2.setpName("DELL");
//        listData.add(p2);
//
//        ParentObj p6 = new ParentObj();
//        p6.setpName("Samsung");
//        listData.add(p6);
//    }
//
//    private void createChildData() {
//        // preparing laptops collection(child)
//        String[] hpModels = { "HPPavilion", "ProBook", "HP" };
//        String[] dellModels = { "Inspiron", "Vostro", "XPS" };
//        String[] samsungModels = { "NPSeries", "Series5", "SFSeries" };
//
//        for (ParentObj p : listData) {
//            if (p.getpName().equals("HP")) {
//                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
//                for (int i = 0; i < hpModels.length; i ++) {
//                    ChildObj cObj = new ChildObj();
//                    cObj.setcName(hpModels[i]);
//                    clists.add(cObj);
//                }
//                p.setChilds(clists);
//            } else if (p.getpName().equals("DELL")) {
//                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
//                for (int i = 0; i < dellModels.length; i ++) {
//                    ChildObj cObj = new ChildObj();
//                    cObj.setcName(dellModels[i]);
//                    clists.add(cObj);
//                }
//                p.setChilds(clists);
//            } else {
//                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
//                for (int i = 0; i < samsungModels.length; i ++) {
//                    ChildObj cObj = new ChildObj();
//                    cObj.setcName(samsungModels[i]);
//                    clists.add(cObj);
//                }
//                p.setChilds(clists);
//            }
//        }
//    }

    /*============================================================*/

//    private List<String> getData(){
//        List<String> data = new ArrayList<String>();
//        data.add("Toast");
//        data.add("AlertDialog");
//        data.add("PopupWindow");
//        return data;
//    }

}
